#!/usr/bin/perl

use Cwd;
use strict;

# usage:
# ./dac_repeat.pl option parameter ...
# where option parameter can be
# --task task_name = which task to run over all files, only one task can be given
# --repeat parameter = a parameter to repeat over, such as in or vis
# --repeat-freq source = the source that should be repeated over all frequencies
# --freq-list freqs = the frequencies to repeat over, a comma separated list
# --pass arg = an argument to pass directly to the miriad task (not required)
# --args-file file = get args to pass to miriad from a file depending on the
#                    frequency. this file should have the frequency as the first
#                    thing on each line, followed by the arguments to pass for that
#                    frequency.

# for example, to run mfcal on all the 1934-638 data
# ./dac_repeat.pl --task mfcal --repeat vis --repeat-freq 1934-638 interval=0.1 refant=3
# note that --pass is not needed, since any arguments not associated with an option
# will be passed anyway

# for example, to run gpcopy from 1934-638 to 0515-674
# ./dac_repeat.pl --task gpcopy --repeat vis --repeat-freq 1934-638 --repeat out --repeat-freq 0515-674
# note how the first --repeat-freq will be associated with the first --repeat
# and the second --repeat-freq will be associated with the second --repeat etc.

# the global variables
my %run_parameters;
my $repeat_variable=1;
my $repeat_source=1;
my @freq_list;
my $freq_list_specified=0;
my $n_passthru=0;
my $give_pause=0;
my $display_only=0;
my $script_output=0;

# ok go through the arguments
for (my $i=0;$i<=$#ARGV;$i++){
    if ($ARGV[$i] eq "--task"){
	# we get the task to run
	$i++;
	$run_parameters{"miriad_task"}=$ARGV[$i];
    } elsif ($ARGV[$i] eq "--repeat"){
	# get a repeat variable
	$i++;
	$run_parameters{"repeat_variable_$repeat_variable"}=$ARGV[$i];
	$repeat_variable++;
    } elsif ($ARGV[$i] eq "--repeat-freq"){
	# get a source to repeat over
	$i++;
	$run_parameters{"repeat_source_$repeat_source"}=$ARGV[$i];
	$repeat_source++;
    } elsif ($ARGV[$i] eq "--freq-list"){
	# the list of frequencies to loop over
	$i++;
	@freq_list=split(/\,/,$ARGV[$i]);
	$freq_list_specified=1;
    } elsif ($ARGV[$i] eq "--pass"){
	# do nothing
    } elsif ($ARGV[$i] eq "--pause"){
	# ask for user to press a key between each command
	$give_pause=1;
    } elsif ($ARGV[$i] eq "--display-only"){
	# the user just wants to see what would happen
	$display_only=1;
    } elsif ($ARGV[$i] eq "--script"){
	# the user wants output suitable for putting in a script
	$display_only=1;
	$script_output=1;
    } elsif ($ARGV[$i] eq "--args-file"){
	# the user wants us to pass arguments conditional on the
	# frequency
	$i++;
	$run_parameters{"arguments_file"}=$ARGV[$i];
    } else {
	# a parameter to pass straight to the miriad task
	$n_passthru++;
	$run_parameters{"pass_$n_passthru"}=$ARGV[$i];
    }
}

# make sure our parameters make sense
if ($repeat_variable!=$repeat_source){
    # can't proceed, as we don't know how to properly repeat
    die "must have the same number of --repeat and --repeat-freq arguments!\n";
}

# do we have any repeats at all?
if ($repeat_variable==1){
    # no!
    die "must have at least one repeating variable!\n";
}

# have we been given the list of frequencies to use?
if ($freq_list_specified==0){
    # no, so we have to get them from the first source
    if ($run_parameters{"repeat_source_1"}!~/\%f/){
	# the location of the frequency has not been specified, so
	# we assume it is the extension of the source name
	
	# check if there is a comma on the command line
	my $check_source=$run_parameters{"repeat_source_1"};
	if ($run_parameters{"repeat_source_1"}=~/\,/){
	    # there is a combination of sources, take the first one
	    $check_source=~s/^(.*?)\,(.*)$/$1/;
	}
	my $ls_command="ls -d ".$check_source.".*";
	open(LS,"-|")||exec $ls_command;
	while(<LS>){
	    chomp;
	    my $frequency=$_;
	    my $getsource=$check_source;
	    $getsource=~s/\+/\\\+/g;
	    $frequency=~s/$getsource\.(.*)/$1/;
	    push @freq_list,$frequency;
	}
	close(LS);
    } else {
	# the location of the frequency has been specified
	
	# check if there is a comma on the command line
	my $check_source=$run_parameters{"repeat_source_1"};
	if ($run_parameters{"repeat_source_1"}=~/\,/){
	    # there is a combination of sources, take the first one
	    $check_source=~s/^(.*?)\,(.*)$/$1/;
	}
	my $ls_command="ls -d ".$check_source;
	$ls_command=~s/\%f/*/g;
	open(LS,"-|")||exec $ls_command;
	while(<LS>){
	    chomp;
	    my $frequency=$_;
	    my $preface=$check_source;
	    $preface=~s/(.*)\%f(.*)/$1/;
	    my $suffix=$check_source;
	    $suffix=~s/(.*)\%f(.*)/$2/;
	    $frequency=~s/$preface(.*?)$suffix/$1/;
	    push @freq_list,$frequency;
	}
	close(LS);
    }
}

my @conditional_freqs;
my @conditional_args;

# are we getting frequency-conditional arguments
if ($run_parameters{"arguments_file"}){
    # yes, so we try to open the file
    open(FREQARGS,$run_parameters{"arguments_file"}) or
	die "cannot open file ".$run_parameters{"arguments_file"}.": $!\n";
    while(<FREQARGS>){
	chomp;
	my $line=$_;
	# get rid of comments and leading spaces
	$line=~s/^\s*//;
	$line=~s/\#.*$//;
	if ($line ne ""){
	    my $mfreq=$line;
	    $mfreq=~s/^(.*?)\s(.*)/$1/;
	    my $margs=$line;
	    $margs=~s/^(.*?)\s(.*)/$2/;
	    push @conditional_freqs,$mfreq;
	    push @conditional_args,$margs;
	}
    }
    close(FREQARGS);
}

# now go through and repeat the commands
# make the preamble of the command first
my $miriad_preamble=$run_parameters{"miriad_task"};
for (my $i=1;$i<=$n_passthru;$i++){
    $miriad_preamble.=" \"".$run_parameters{"pass_$i"}."\"";
}
for (my $i=0;$i<=$#freq_list;$i++){
    my $miriad_command=$miriad_preamble;
    # add the frequency-conditional args
    for (my $j=0;$j<=$#conditional_freqs;$j++){
	if ($conditional_freqs[$j]==$freq_list[$i]){
	    $miriad_command .= " ".$conditional_args[$j];
	}
    }
    for (my $j=1;$j<$repeat_variable;$j++){
	$miriad_command.=" ".$run_parameters{"repeat_variable_$j"};
	my $sourcename=$run_parameters{"repeat_source_$j"};
	if ($sourcename=~/\%f/){
	    # the user wants us to put the frequency in a specific spot
	    $sourcename=~s/\%f/$freq_list[$i]/g;
	} else {
	    # just put the frequency at the end of the name
	    $sourcename.=".".$freq_list[$i];
	    # and replace any commas with the frequency as well
	    $sourcename=~s/\,/$freq_list[$i]\,/g;
	}
	$miriad_command.="=".$sourcename;
    }
    if ($script_output==0){
	print "working directory is ".cwd()."\n";
	print "executing [$miriad_command]\n";
    } else {
	print "$miriad_command\n";
    }
    if ($display_only==0){
	system "$miriad_command";
    }
    if ($give_pause==1){
	if ($script_output==0){
	    print "[dac_repeat] waiting for user to press ENTER...";
	    my $temp=<STDIN>;
	}
    }
}
